<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="APP版本管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="600px"
    >
        <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="应用" prop="appId">
                <el-select v-model="form.appId" filterable placeholder="请选择" clearable style="width:200px;">
                    <el-option
                        v-for="item in Dict.enums.appList"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="版本号" prop="version">
                <el-input v-model="form.version" placeholder="请输入"/>
            </el-form-item>

            <el-form-item label="上传文件" prop="apkUrl">
                <el-input v-model="form.apkUrl" placeholder="请输入">
                    <template #append>
                        <el-upload
                            :action="apiHost + '/file/upload'"
                            :on-success="handleSuccess"
                            :before-upload="handleBeforeUpload"
                            :headers="headers"
                            :show-file-list="false"
                        >
                            <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>

                    </template>
                </el-input>
            </el-form-item>

            <el-form-item label="更新说明" prop="description">
                <el-input v-model="form.description" type="textarea" placeholder="请输入"/>
            </el-form-item>

            <el-form-item label="强制更新" prop="forced">
                <el-switch v-model="form.forced"/>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false" style="margin-right: 20px;">取 消</el-button>
            <el-popconfirm title="确定保存吗？" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">保存</el-button>
            </el-popconfirm>
        </div>
    </el-dialog>
</template>

<script>
import {AppVersionApi} from '@/api/system/AppVersionApi'
import {getToken} from "@/utils/auth";

export default {
    name: 'AppVersionUpdate',
    components: {},
    data() {
        return {
            form: {
                id: '',
                appId:'',
                version:'',
                apkUrl:'',
                description:'',
                forced:false,
                content: '',
            },
            rules: {


            },
            dialogVisible: false,
            headers: {'X-Token': getToken()},
            apiHost: process.env.VUE_APP_BASE_API,
            loading:null
        }
    },
    created() {

    },
    methods: {
        // 显示组件
        async showUpdate(row) {
            const that = this

            that.dialogVisible = true
            if (that.$refs['formRef'] !== undefined) {
                that.$refs['formRef'].resetFields()
            }

            that.$nextTick(() => {
                if (row) {
                    that.form = Object.assign({}, row)
                }else{
                    this.form.id = ""
                }
            })
        },

        handleBeforeUpload(){
            this.loading = this.$loading({
                lock: true,
                text: '文件上传中，请稍后...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
        },

        handleSuccess(e) {
            this.loading.close()
            this.form.apkUrl = e.data
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (!valid) return false

                if (that.form.id) {
                    AppVersionApi.edit(that.form).then(() => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                } else {
                    AppVersionApi.create(that.form).then(() => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                }
            })
        }
    }
}
</script>


